<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="./jessibuca.js"></script>
    <style>
        .root {
            display: flex;
            place-content: center;
            margin-top: 3rem;
        }

        .container-shell {
            backdrop-filter: blur(5px);
            background: hsla(0, 0%, 50%, 0.5);
            padding: 30px 4px 10px 4px;
            /* border: 2px solid black; */
            width: auto;
            position: relative;
            border-radius: 5px;
            box-shadow: 0 10px 20px;
        }

        .container-shell:before {
            content: "jessibuca demo player";
            position: absolute;
            color: darkgray;
            top: 4px;
            left: 10px;
            text-shadow: 1px 1px black;
        }

        .container {
            background: rgba(13, 14, 27, 0.7);
            width: 320px;
            height: 199px;
            display: inline-block;
            margin-right: 10px;
            margin-bottom: 10px;
        }

        #container {

        }

        .input {
            display: flex;
            margin-top: 10px;
            color: white;
            place-content: stretch;
        }

        .input2 {
            bottom: 0px;
        }

        .input input {
            flex: auto;
        }

        .err {
            position: absolute;
            top: 40px;
            left: 10px;
            color: red;
        }

        .option {
            position: absolute;
            top: 4px;
            right: 10px;
            display: flex;
            place-content: center;
            font-size: 12px;
        }

        .option span {
            color: white;
        }

        .page {
            background: url('./bg.jpg');
            background-repeat: no-repeat;
            background-position: top;
        }

        @media (max-width: 720px) {
            .container {
                width: 90vw;
                height: 52.7vw;
            }
        }
    </style>
</head>
<body class="page">
<div class="root">

    <div class="container-shell">
        <div>
            <div class="container" id="container1"></div>
            <div class="container" id="container2"></div>
            <div class="container" id="container3"></div>
        </div>
        <div>
            <div class="container" id="container4"></div>
            <div class="container" id="container5"></div>
            <div class="container" id="container6"></div>
        </div>
        <div>
            <div class="container" id="container7"></div>
            <div class="container" id="container8"></div>
            <div class="container" id="container9"></div>
        </div>
        <div class="input" style="width: 1000px;flex-direction: column">
            <div>
                js为单线程运行时, 无法通过多线程提升并发性能.
            </div>
            <div>将音视频解码在worker中进行, 能有效利用多核处理器提升多画面播放的流畅度.</div>
            <div>
                chrome限制同源http请求最多6个并发, 因此超过6个并发请求我们采用websocket-flv来播放
            </div>
            <div>请准备M7S服务端来测试. worker js不能跨域调用,</div>
        </div>
        <div class="input">
            <div>输入URL：</div>
            <input
                autocomplete="on"
                id="playUrl1"
                value=""
            />
        </div>
        <div class="input">
            <div>输入URL：</div>
            <input
                autocomplete="on"
                id="playUrl2"
                value=""
            />
        </div>
        <div class="input">
            <div>输入URL：</div>
            <input
                autocomplete="on"
                id="playUrl3"
                value=""
            />
        </div>
        <div class="input">
            <div>输入URL：</div>
            <input
                autocomplete="on"
                id="playUrl4"
                value=""
            />
        </div>
        <div class="input">
            <div>输入URL：</div>
            <input
                autocomplete="on"
                id="playUrl5"
                value=""
            />
        </div>
        <div class="input">
            <div>输入URL：</div>
            <input
                autocomplete="on"
                id="playUrl6"
                value=""
            />
        </div>
        <div class="input">
            <div>输入URL：</div>
            <input
                autocomplete="on"
                id="playUrl7"
                value=""
            />
        </div>
        <div class="input">
            <div>输入URL：</div>
            <input
                autocomplete="on"
                id="playUrl8"
                value=""
            />
        </div>
        <div class="input">
            <div>输入URL：</div>
            <input
                autocomplete="on"
                id="playUrl9"
                value=""
            />
        </div>
        <div class="input">
            <button id="play">播放</button>
            <button id="pause" style="display: none">停止</button>
        </div>
        <div class="input" style="line-height: 30px">
            <button id="destroy">销毁</button>
        </div>
    </div>
</div>

<script>
    var $player = document.getElementById('play');
    var $pause = document.getElementById('pause');
    var $destroy = document.getElementById('destroy');


    var showOperateBtns = true; // 是否显示按钮
    var forceNoOffscreen = true; //
    var playList = [];
    var size = 9;

    function create(id, index) {
        var $container = document.getElementById('container' + id);

        var jessibuca = new Jessibuca({
            container: $container,
            videoBuffer: 0.2, // 缓存时长
            isResize: false,
            text: "",
            loadingText: "加载中",
            debug: false,
            showBandwidth: showOperateBtns, // 显示网速
            operateBtns: {
                fullscreen: showOperateBtns,
                screenshot: showOperateBtns,
                play: showOperateBtns,
                audio: showOperateBtns,
            },
            forceNoOffscreen: forceNoOffscreen,
            isNotMute: false,
            websocketOpenTimeout: 3 + index * 0.5,
            loadingTimeout: 5 + index * 0.5,
        },);

        jessibuca.onLog = msg => console.error(msg);
        jessibuca.onRecord = (status) => console.log('onRecord', status);
        jessibuca.onPause = () => console.log('onPause');
        jessibuca.onPlay = () => console.log('onPlay');
        jessibuca.onFullscreen = msg => console.log('onFullscreen', msg);
        jessibuca.onMute = msg => console.log('onMute', msg);
        playList.push(jessibuca);
    }


    for (let i = 0; i < size; i++) {
        create(i + 1, i);
        $player.style.display = 'inline-block';
        $pause.style.display = 'none';
        $destroy.style.display = 'none';
    }


    $player.addEventListener('click', function () {
        for (let i = 0; i < size; i++) {
            var id = i + 1;
            var $playHref = document.getElementById('playUrl' + id);
            let player = playList[i];
            if ($playHref.value) {
                setTimeout((url) => {
                    console.log(url);
                    player && player.play(url).then(() => {

                    }).catch((e) => {
                        console.error(e);
                    });
                }, 0, $playHref.value)
            }
        }


        $player.style.display = 'none';
        $pause.style.display = 'inline-block';
        $destroy.style.display = 'inline-block';
    }, false)


    $pause.addEventListener('click', function () {
        $player.style.display = 'inline-block';
        $pause.style.display = 'none';
        for (let i = 0; i < size; i++) {
            let player = playList[i];
            player && player.pause()
        }
    })

    $destroy.addEventListener('click', function () {
        const destroyList = [];
        for (let i = 0; i < size; i++) {
            let player = playList[i];
            if (player) {
                destroyList.push(player.destroy());
            }
        }
        Promise.all(destroyList).then(() => {
            playList = [];
            setTimeout(() => {
                for (let i = 0; i < size; i++) {
                    create(i + 1);
                }
                $player.style.display = 'inline-block';
                $pause.style.display = 'none';
                $destroy.style.display = 'none';
            }, 100)
        }).catch((e) => {
            console.error(e);
        })


    })

</script>

</body>
</html>
